﻿<?php
	require_once(dirname(__FILE__) . "/../core/safeParam.php");
	require_once("facebook_phpSdk/src/facebook.php");
	
	define('FACEBOOK_APP_ID', '423263144390114');
	define('FACEBOOK_SECRET', '9d7e1f8c0dc393f3938bcc1d1595c19a');
	
	global $database;
	$estado = 0;

	function parse_signed_request($signed_request, $secret) {
  		list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

  		// decode the data
  		$sig = base64_url_decode($encoded_sig);
  		$data = json_decode(base64_url_decode($payload), true);

  		if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
    			error_log('Unknown algorithm. Expected HMAC-SHA256');
    			return null;
  		}

  		// check sig
  		$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  		if ($sig !== $expected_sig) {
    			error_log('Bad Signed JSON signature!');
    			return null;
  		}

  		return $data;
	}

	function base64_url_decode($input) {
    		return base64_decode(strtr($input, '-_', '+/'));
	}
	
	$smarty->assign("urlpath", $urlpath);
	
	if ($_REQUEST) {
  	   $response = parse_signed_request($_REQUEST['signed_request'], FACEBOOK_SECRET);

	   $name 	= $response['registration']['name'];
	   $arrayName	= explode(' ', $name);
	  	
	   $nombre	= $arrayName[0];
	   $apellido	= $arrayName[1];
	   $email 	= $response['registration']['email'];
	   $celular 	= $response['registration']['celular'];
	   $direccion 	= $response['registration']['direccion'];
	   $username    = $response['registration']['username'];
	   $password	= $response['registration']['password'];
	       	
	   //REGISTRO USUARIO DESDE FACEBOOK
	   $check_user_exists = $database->select(" SELECT id FROM users where email='". $email ."' ");
	   
	   if ($check_user_exists != FALSE) {   
	   	$estado = 0;	
			$smarty->assign("estado", $estado);
  	   } else {
  	   	if($nombre != '') {
  	   		$user = array();
  	   		$user['name']      	= $nombre;
    	      $user['email']     	= $email;
    	      $user['active']		= 0;
    	      $user['last_name'] 	= $apellido;
    	      $user['address']   	= $direccion;
    	      $user['address2']   	= "";
    	      $user['city']      	= "Cordoba";
    	      $user['state']     	= "Cordoba";
    	      $user['phone']     	= $celular; 
    	      $user['special']		= "";
    	      $user['password']  	= md5($password);
    	      $user['zip']			= "";
    	      $user['rid']			= "";
    	      $user['activation_time']	= "";
    	      
    	      //insert user  
    	      $database->insert_array('users', $user);
    	      
    	      //mail
    	      $msgBody ='
    	      <html>
	    			<head>
    					<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    					<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    				</head>
    			
    				<body>
    					<h3><b>Bienvenido a Delivery351.com</b></h3>
    				
    					<p>
    						Estimado <b>'. $user['name'] .'</b>
    				
    						<br /><br />
    				
    						Gracias por registrarte con nosotros. 
    						<br />
    						Tu nueva cuenta ha sido configurada, puedes iniciar sesión en nuestra área de miembros usando los siguientes datos.
    				
    						<br /><br />
    				
    						Usuario: <b>'. $user['email'] .'</b>
    						<br />    				
    						Contraseña: <b>'. $password .'</b>
    				
    						<br /><br />
    					
    						Para iniciar sesión, <a href="http://www.delivery351.com/index.php?act=reg">click aquí</a>
    			
    						<br /><br />
    						<i>Delivery351</i>
    					</p>
    				</body>
    			</html>
    			';   
    		
    			//require_once('configMail.php');
    			require_once('phpmailer/class.phpmailer.php');
    			
    			$mail             = new PHPMailer(); 				// defaults to using php "mail()"
    			$mail->SetFrom('info@delivery351.com', 'info@delivery351.com');
    			$mail->AddReplyTo("info@delivery351.com","info@delivery351.com");
    			$mail->Subject    = "Registro en Delivery351";
    			
    			$address = $user['email'];
    			$mail->AddAddress($address);
    			$mail->AddBCC("patricioa183@hotmail.com"); 

    			$mail->MsgHTML($msgBody);
    		
    			$mail->send();
    			
    			$estado = 1;
    			$smarty->assign("estado", $estado);
    			
    			header("Location http://www.delivery351.com");
  	   	}    	      
  	   }       		
	} else {
		$estado = 2;
		$smarty->assign("estado", $estado);
	}
?>